﻿package mortal.game.view.forging.view.compound
{
    import Message.DB.Tables.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.component.GCatogeryList.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.forging.view.*;
    import mortal.mvc.core.*;

    public class CompoundPanel extends ForgingPanelBase
    {
        private var _onlyUseUnBind:Boolean = false;
        private var _isOnlyShowCanComp:Boolean = false;
        private var _ItemListPanel:GCatogeryList;
        private var _leftPart:CompoundLeftPart;
        private var _onlyUseUnBindBox:GCheckBox;
        private var _onlyShowCanComp:GCheckBox;
        private var _info:TCompoundConfig;
        private var _selectIndex:int;
        public static var curItem:ItemData;

        public function CompoundPanel(param1:Window)
        {
            super(param1);
            return;
        }// end function

        private function addListener() : void
        {
            Dispatcher.addEventListener(EventName.CompoundInfoSelect, this.updateSelectCompound);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.resetDataProvider);
            NetDispatcher.addCmdListener(ServerCommand.ComPoundScuss, this.playCompoundSuccessHandler);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpHandler);
            return;
        }// end function

        private function removeListerner() : void
        {
            Dispatcher.removeEventListener(EventName.CompoundInfoSelect, this.updateSelectCompound);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.resetDataProvider);
            NetDispatcher.removeCmdListener(ServerCommand.ComPoundScuss, this.playCompoundSuccessHandler);
            Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.levelUpHandler);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.SynthesisPanelRightBg, 495, 2, this));
            this.pushUIToDisposeVec(UIFactory.bg(488, 392, 293, 1, this, ImagesConst.SplitLine));
            this._leftPart = UICompomentPool.getUICompoment(CompoundLeftPart);
            this.addChild(this._leftPart);
            this._ItemListPanel = UICompomentPool.getUICompoment(GCatogeryList);
            this._ItemListPanel.setListSize(246, 375);
            this._ItemListPanel.x = 490 + 25;
            this._ItemListPanel.y = 4;
            this._ItemListPanel.tileList.direction = GBoxDirection.VERTICAL;
            this._ItemListPanel.tileList.columnWidth = 230;
            this._ItemListPanel.tileList.columnCount = 1;
            this._ItemListPanel.headGap = 0;
            this.addChild(this._ItemListPanel);
            this._ItemListPanel.scrollPane.verticalScrollPolicy = ScrollPolicy.ON;
            this._onlyUseUnBindBox = UIFactory.checkBox(Language.getString(41065), 15, 400, 210, 28, this);
            this._onlyUseUnBindBox.selected = this._onlyUseUnBind;
            this._onlyUseUnBindBox.configEventListener(Event.CHANGE, this.useBindItemHandler);
            this._onlyShowCanComp = UIFactory.checkBox(Language.getString(41066), 565, 400, 210, 28, this);
            this._onlyShowCanComp.selected = this._isOnlyShowCanComp;
            this._onlyShowCanComp.configEventListener(Event.CHANGE, this.changeOnlyShowCanComp);
            this.addListener();
            this.initLeftList();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.removeListerner();
            this._leftPart.dispose(param1);
            this._leftPart = null;
            this._ItemListPanel.dispose(param1);
            this._ItemListPanel = null;
            this._onlyUseUnBindBox.dispose(param1);
            this._onlyUseUnBindBox = null;
            this._onlyShowCanComp.dispose(param1);
            this._onlyShowCanComp = null;
            this._info = null;
            curItem = null;
            this._selectIndex = -1;
            super.disposeImpl(param1);
            return;
        }// end function

        private function levelUpHandler(event:Event) : void
        {
            this.resetDataProvider(false);
            return;
        }// end function

        private function useBindItemHandler(event:Event) : void
        {
            this._onlyUseUnBind = this._onlyUseUnBindBox.selected;
            this._leftPart.resetIsUseBindItem(this._onlyUseUnBind);
            this.resetDataProvider(false);
            return;
        }// end function

        private function changeOnlyShowCanComp(event:Event) : void
        {
            this._isOnlyShowCanComp = this._onlyShowCanComp.selected;
            this.resetDataProvider(false);
            return;
        }// end function

        private function updateSelectCompound(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._selectIndex = _loc_2.index;
            this._info = _loc_2.data as TCompoundConfig;
            this._leftPart.updateInfo(this._info, this._onlyUseUnBind, _loc_2.maxNum);
            return;
        }// end function

        public function initLeftList() : void
        {
            var _loc_1:int = 23;
            var _loc_2:* = CompoundConfig.instance.carogertTabData;
            var _loc_3:* = _loc_1 * _loc_2.length;
            this._ItemListPanel.createHeads(CompoundCatogeryHead, _loc_2, 230, _loc_1);
            this._ItemListPanel.setCellRender(0, CompoundListRender, true);
            this._ItemListPanel.setCellHeight(0, _loc_1, true);
            this._ItemListPanel.setListItemHeight(_loc_1);
            this.resetDataProvider(true);
            return;
        }// end function

        private function getIndexByDataProvider(param1:DataProvider) : int
        {
            var _loc_2:TCompoundConfig = null;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_3:int = -1;
            while (_loc_4 < param1.length)
            {
                
                _loc_5 = param1.getItemAt(_loc_4);
                if (_loc_5)
                {
                    _loc_2 = _loc_5.info as TCompoundConfig;
                    if (_loc_2 && _loc_2.id == this._info.id)
                    {
                        _loc_3 = _loc_4;
                        break;
                    }
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

        private function getNeedCompoundItem(param1:DataProvider) : int
        {
            var _loc_2:TCompoundConfig = null;
            var _loc_4:Array = null;
            var _loc_5:Array = null;
            var _loc_6:String = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:Object = null;
            var _loc_3:int = -1;
            while (_loc_8 < param1.length)
            {
                
                _loc_9 = param1.getItemAt(_loc_8);
                if (_loc_9)
                {
                    _loc_2 = _loc_9.info as TCompoundConfig;
                    _loc_5 = _loc_2.recipe.split("&");
                    for each (_loc_6 in _loc_5)
                    {
                        
                        _loc_4 = _loc_6.split("#");
                        _loc_7 = _loc_4[0];
                        if (_loc_7 == curItem.itemCode || _loc_7 == curItem.itemInfo.codeUnbind)
                        {
                            _loc_3 = _loc_8;
                            return _loc_3;
                        }
                    }
                }
                _loc_8++;
            }
            return _loc_3;
        }// end function

        public function resetDataProvider(param1:Boolean = true) : void
        {
            var _loc_3:DataProvider = null;
            var _loc_4:int = 0;
            var _loc_2:* = CompoundConfig.instance.carogertTabData;
            this._selectIndex = -1;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_3 = this.getDataProvider(CompoundConfig.instance.compoundDic[_loc_2[_loc_5].type]);
                this._ItemListPanel.setDataProvider(_loc_5, _loc_3, param1);
                if (this._selectIndex < 0)
                {
                    if (curItem)
                    {
                        this._selectIndex = this.getNeedCompoundItem(_loc_3);
                        if (this._selectIndex >= 0)
                        {
                            _loc_4 = _loc_2[_loc_5].type - 1;
                            param1 = false;
                        }
                    }
                    else if (this._info && !param1 && this._info.type == _loc_2[_loc_5].type)
                    {
                        _loc_4 = this._info.type - 1;
                        this._selectIndex = this.getIndexByDataProvider(_loc_3);
                    }
                }
                _loc_5++;
            }
            curItem = null;
            if (!param1)
            {
                this._ItemListPanel.expandItem(_loc_4, this._selectIndex);
            }
            return;
        }// end function

        private function getDataProvider(param1:Array) : DataProvider
        {
            var _loc_4:TCompoundConfig = null;
            var _loc_5:int = 0;
            var _loc_6:ItemData = null;
            var _loc_7:int = 0;
            var _loc_8:Array = null;
            var _loc_9:int = 0;
            var _loc_2:* = new DataProvider();
            var _loc_3:* = new Object();
            while (_loc_7 < param1.length)
            {
                
                _loc_4 = param1[_loc_7];
                _loc_8 = _loc_4.output.split("#");
                _loc_9 = _loc_8[0];
                _loc_6 = new ItemData(_loc_9);
                if (ItemsUtil.isFitCarrer(_loc_6) && ItemsUtil.isEnoughLevel(_loc_6))
                {
                    _loc_5 = this.canCompoundNum(_loc_4);
                    if (!this._isOnlyShowCanComp || _loc_5 > 0)
                    {
                        _loc_3 = {name:_loc_4.outputName, index:_loc_7, info:_loc_4, canCompoundNum:_loc_5};
                        _loc_2.addItem(_loc_3);
                    }
                }
                _loc_7++;
            }
            return _loc_2;
        }// end function

        private function canCompoundNum(param1:TCompoundConfig) : int
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Boolean = false;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:ItemInfo = null;
            var _loc_10:int = 0;
            var _loc_2:* = param1.recipe.split("&");
            while (_loc_10 < _loc_2.length)
            {
                
                _loc_3 = (_loc_2[_loc_10] as String).split("#");
                _loc_4 = _loc_3[0];
                _loc_7 = _loc_3[1];
                _loc_9 = ItemConfig.instance.getConfig(_loc_4);
                _loc_5 = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_9.codeUnbind, !this._onlyUseUnBind);
                if (_loc_5 >= int(_loc_3[1]))
                {
                    if (_loc_8 == 0 || _loc_8 > _loc_5 / _loc_7)
                    {
                        _loc_8 = _loc_5 / _loc_7;
                    }
                }
                else
                {
                    _loc_8 = 0;
                    break;
                }
                _loc_10++;
            }
            return _loc_8;
        }// end function

        public function playCompoundSuccessHandler(param1:Object) : void
        {
            this._leftPart.playCompoundSuccessHandler();
            return;
        }// end function

    }
}
